using FSM.IDal; 
using FSM.IBll; 
using FSM.Model; 
using SqlSugar; 
using System; 
using System.Collections.Generic; 
using System.Text;
using System.Linq;

namespace FSM.Bll 
{ 
    public class SysRoleBll: ISysRoleBll 
    { 
        #region 模块 
        private readonly ISysRoleDal dal; 
        public SysRoleBll(ISysRoleDal dal) 
        { 
            this.dal = dal; 
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="role_no">键值</param> 
        /// <returns></returns> 
        public sys_role Get(string role_no) 
        { 
            return dal.GetById(role_no); 
        } 
 
        /// <summary> 
        /// 查询批量信息 
        /// </summary> 
        /// <param name="role_no">角色编号</param> 
        /// <param name="role_name">角色名称</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<sys_role>, int> List(string role_no, string role_name, int page, int page_size) 
        { 
            ISugarQueryable<sys_role> queryable = dal.CreateQueryable(); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(role_no),p => p.role_no == role_no); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(role_name),p => p.role_name == role_name); 
             
            PageModel pm = new PageModel 
            { 
                PageIndex = page, 
                PageSize = page_size 
            }; 
            Tuple<List<sys_role>, int> result = dal.GetPageListAndTotal(queryable, pm); 
            return result; 
        } 
 
        #endregion 
 
        #region 提交 
 
        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Insert(sys_role model) 
        { 
            model.create_date = DateTime.Now; 
            return dal.Insert(model); 
        } 
 
        /// <summary> 
        /// 更新 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Update(sys_role model) 
        {
            if (model.role_no.ToLower() == "administrators")
            {
                throw new Exception("你没有权限修改此角色");
            }
            model.edit_date = DateTime.Now; 
            return dal.UpdateExcludeColumns(model, "creater", "create_date") == 1; 
        } 
 
        /// <summary> 
        ///  删除 
        /// </summary> 
        /// <param name="role_no">键值</param> 
        /// <returns></returns> 
        public bool Delete(params string[] role_no_arr) 
        {
            int iCount = role_no_arr.Count(p => p.ToLower() == "administrators");
            if (iCount > 0)
            {
                throw new Exception("你没有权限删除角色【Administrators】");
            }
            return dal.Delete(role_no_arr); 
        } 
        #endregion 
    } 
} 
